import { workModulePageSelect } from '@/api/order'
import { ref } from 'vue'

/**
 * 工单模块下拉选项列表服务
 */
export default function () {
  const getOrderModuleOptionListLoading = ref(false)
  const getOrderModuleOptionListParam = ref({
    limit: 50,
    name: '',
    page: 1,
    factoryId: '',
  })
  const orderModuleOptionList = ref<Array<OrderAPIResponse.OrderModuleOptionListItem>>([])

  const resetGetOrderModuleOptionListParam = () => {
    getOrderModuleOptionListParam.value = {
      limit: 50,
      name: '',
      page: 1,
      factoryId: '',
    }
  }

  const getOrderModuleOptionList = async () => {
    try {
      getOrderModuleOptionListLoading.value = true
      const res = await workModulePageSelect(getOrderModuleOptionListParam.value)
      getOrderModuleOptionListLoading.value = false
      if (res.data.code === 20000) {
        orderModuleOptionList.value = res.data.data.rows
        return ''
      }

      return res.data.message
    } catch (error) {
      getOrderModuleOptionListLoading.value = false
      return '获取工单模块下拉选项列表失败，请稍后重试'
    }
  }

  return {
    orderModuleOptionList,
    getOrderModuleOptionListParam,
    getOrderModuleOptionListLoading,
    getOrderModuleOptionList,
    resetGetOrderModuleOptionListParam,
  }
}
